//
//  AppDelegate.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "AppDelegate.h"
#import "CYTabBarController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self checkToken];
    //初始化环信客服sdk
    //    [self initializeHuanXinSDKWithApplication:application didFinishLaunchingWithOptions:launchOptions];
    //检测版本更新
    //    [self updateVersionWithApplication:application didFinishLaunchingWithOptions:launchOptions];
    
    [self setupCYAudio];//设置声音震动相关设置
    [self setupUser];//设置个人信息
    [self checkNetwork];//监测网络
    [self showBoxView];//请求弹框广告
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    CYTabBarController * tabBarVC = [[CYTabBarController alloc] init];
    self.window.rootViewController = tabBarVC;
    [self.window makeKeyAndVisible];
    return YES;
}

//检查token是否过期，如果过期，删除本地username和token
- (void)checkToken{
    //    DeLog(@"token %@   username  %@",[CYCache readDataWithKey:@"token"],[CYCache readDataWithKey:@"username"]);
    //    [CYHttps POST:API_personal parameters:@{@"username":[CYCache readDataWithKey:@"username"],
    //                                            @"token":[CYCache readDataWithKey:@"token"]}
    //          success:^(id data) {
    //              if ([data[@"code"] integerValue] == 2) {//token过期
    //                  [CYCache deleteDataWithKey:@"username"];
    //                  [CYCache deleteDataWithKey:@"token"];
    //              }
    //          } failure:nil];
}

//设置声音震动相关设置
- (void)setupCYAudio{
    [CYAudioManager initializeOpen];//初始化声音震动属性
}

//监测网络
- (void)checkNetwork{
    [CYHttps checkNetwork];
}

//初始化user
- (void)setupUser{
    [CYUser initializeUsernameAndToken];
}

//弹框广告
- (void)showBoxView{
    //    if ([CYUser shareUser].username && [CYUser shareUser].token)
    //        [CYHttps POST:API_ad parameters:@{@"username":[CYUser shareUser].username,@"token":[CYUser shareUser].token} success:^(id data) {
    //            [CYPopBoxView initWithModel:[CYBoxModel mj_objectWithKeyValues:data]];
    //        } failure:nil];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)openURL:(NSString *)url{
    if ([ [UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]){   // 看是否 允许跳转
#ifdef iOS10Later
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
#else
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
#endif
        return YES;
    }
    return NO;
}

@end
